package teacher.action;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class downCom extends ActionSupport {

	private static final long serialVersionUID = 1L;
//下面以中文名文件实例
 //此处文件名称由用户输入，此处也是动态传参的过程
	private String nameCom;

 public String execute() throws Exception {

	 String uploadPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
	 String filePath=uploadPath+File.separator+this.nameCom;
	 File dir=new File(filePath); 
	 if(!dir.exists())
	 {
		 return ERROR;
		 }
  return super.execute();
 }
 
 public String getNameCom() {
	 String downFileName = nameCom;	 

	 try {

	 downFileName = new String(nameCom.getBytes(), "ISO8859-1");

	 } catch (UnsupportedEncodingException e) {
		 
	 e.printStackTrace();

	 }

	 return downFileName;

	 }
 
 public InputStream getDownLoad() throws UnsupportedEncodingException {
	 
  System.out.println("/upload/"+nameCom);

  return ServletActionContext.getServletContext().getResourceAsStream(
    "/upload/"+nameCom);

 }

	public void setNameCom(String nameCom)
			throws UnsupportedEncodingException {
			this.nameCom = new String(nameCom.getBytes("ISO8859-1"), "utf-8");
			}

}
